package com.kaikai.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaikai.commonutils.JwtUtils;
import com.kaikai.commonutils.R;
import com.kaikai.eduorder.client.CourseClient;
import com.kaikai.eduorder.client.UcenterClient;
import com.kaikai.eduorder.entity.Order;
import com.kaikai.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vo.CommentUserInfo;
import vo.EduCourseOrder;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author yujiankai
 * @since 2021-06-18
 */
@RestController
@CrossOrigin
@RequestMapping("/eduorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**生成订单,
     * 1.添加数据
     * 2.返回订单编号
     * @param courseId
     * @param request
     * @return
     */
    @GetMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        //必须保证用户登录
        //token值是存在request头中,如果未登录,request头中就没有token值
        try{
        String orderNo= orderService.createOrder(courseId,JwtUtils.getMemberIdByJwtToken(request));
            return R.ok().data("orderNo",orderNo);}
        catch(Exception e){
            return R.error().message("请先登录");
        }
    }
    //根据订单编号返回订单
    @GetMapping("getOrderInfo/{orderNo}")
    public R getOrderInfo(@PathVariable String orderNo){
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("order_no", orderNo);
        Order order = orderService.getOne(wrapper);
       return R.ok().data("order",order);
    }
}

